//
//  MyQueryViewController.m
//  NewApp
//
//  Created by xueyan liu on 7/15/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "MyQueryViewController.h"

#import "NAAccount.h"
#import "PathHelper.h"
#import "ProductData.h"
#import "CustomTableCellD.h"
#import "MyQueryDetailViewController.h"

@interface MyQueryViewController ()

@end

@implementation MyQueryViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    
    [super viewDidLoad];
    [self.view setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"news_list_B_item_bg_1"]]];
    self.bHideBack = NO;
    self.listStyle = @"D";
    self.dataArray = [NSMutableArray arrayWithCapacity:10];
    
    NSString *filePath = [[PathHelper documentDirectoryPathWithName:@"Acc"] 
                          stringByAppendingPathComponent:@"accounts.info"];
    
    NAAccount *acc = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
    if(acc != nil) {
        
        userID = [acc.uid retain];
        [self.engine myQueryWithID:userID];
    }
}

- (void)dealloc {
    
    [userID release];
    
    [super dealloc];
}

#pragma mark -
#pragma mark UITableViewDataSource & UITableViewDelegate Support.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    if (cell == nil) {
        
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        
    }
    // Configure the cell...
    MyOrderEntity *entity = [dataArray objectAtIndex:indexPath.row];
    
    [cell.backgroundView setFrame:CGRectMake(0, 0, 320, 90)];
    cell.backgroundColor=[UIColor clearColor];
    if (indexPath.row % 2 == 0 ) {
        cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"news_list_B_item_bg_1.png"]]; 
    }else {
        cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"news_list_B_item_bg_2.png"]];
    }
    
    UITableViewCell *celltmp;
    if([listStyle isEqualToString:@"D"]) {
        
        celltmp = [[CustomTableCellD alloc] init];
        celltmp.userInteractionEnabled = NO;
        [(CustomTableCellD*)celltmp setContentWithTitle:[NSString stringWithFormat:@"日期:%@ 狀態:%@", entity.date, entity.state]
                                               subTitle:[NSString stringWithFormat:@"訂單單號:%@", entity.ID]
                                              exContent:@""];
    }
    
    [cell addSubview:celltmp];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    MyQueryDetailViewController *viewCtrl = [[MyQueryDetailViewController alloc] initWithNibName:nil bundle:nil];
    viewCtrl.userID = userID;
    viewCtrl.ID = ((MyOrderEntity *)[dataArray objectAtIndex:indexPath.row]).ID;
    [self.navigationController pushViewController:viewCtrl animated:YES];
    [viewCtrl release];
}

#pragma mark -
#pragma mark Request Engine Support.

- (void)requestDidFinished:(RequestEngine *)reqEngine withObject:(id)object {
    
    [dataArray removeAllObjects];
    
    NSArray *results = [(NSDictionary *)object objectForKey:@"data"];
    self.titleLabel.text = [(NSDictionary *)object objectForKey:@"top_title"];
    for (NSDictionary *subDict in results) {
        
        [dataArray addObject:[[MyOrderEntity alloc] initWithDictionary:subDict]];
    }
    
    [dataArray makeObjectsPerformSelector:@selector(release)];
    
    [productTable reloadData];
}

@end
